#include "../skyAdapter.h"

#include "../../adapter/skyPainter.h"

#ifdef _SYMBIAN
#include "../skyPict.h"
#include "../skyFont.h"
#include "../skyCanvas.h"
namespace Sky
	{
//	CImageTransform::CImageTransform()
//	{
//		// No implementation required
//		cBitmapRotator = CMdaBitmapRotator::NewL();
//		iActiveSchedulerWait = new (ELeave)CActiveSchedulerWait;
//	}
//
//
//	CImageTransform::~CImageTransform()
//	{
//		DELETE(cBitmapRotator);
//		if (iActiveSchedulerWait->IsStarted())
//		{
//			iActiveSchedulerWait->AsyncStop();
//		}	
//		DELETE(iActiveSchedulerWait);
//	}
//
//	void CImageTransform::MiuoConvertComplete(TInt aError)
//	{
//		if (iActiveSchedulerWait->IsStarted())
//		{
//			iActiveSchedulerWait->AsyncStop();
//		}
//	}
//
//	void CImageTransform::Rotator(CFbsBitmap* aSrcBitmap, CFbsBitmap* aTgtBitmap, RotatorType type)
//	{
//		if (ROTATOR_90 == type)
//		{
//			cBitmapRotator->RotateL(*this,*aSrcBitmap,*aTgtBitmap,CMdaBitmapRotator::ERotation90DegreesClockwise);
//		}
//		else if (ROTATOR_180 == type)
//		{
//			cBitmapRotator->RotateL(*this,*aSrcBitmap,*aTgtBitmap,CMdaBitmapRotator::ERotation180DegreesClockwise);
//		}
//		else
//		{
//			cBitmapRotator->RotateL(*this,*aSrcBitmap,*aTgtBitmap,CMdaBitmapRotator::ERotation270DegreesClockwise);
//		}
//		iActiveSchedulerWait->Start();	
//	}
//
//	void CImageTransform::Mirror(CFbsBitmap* aSrcBitmap, CFbsBitmap* aTgtBitmap, MirrorType type)
//	{
//		if (HORIZONTAL == type)
//		{
//			cBitmapRotator->RotateL(*this,*aSrcBitmap,*aTgtBitmap,CMdaBitmapRotator::EMirrorFlipHorizontal);
//		}
//		else
//		{
//			cBitmapRotator->RotateL(*this,*aSrcBitmap,*aTgtBitmap,CMdaBitmapRotator::EMirrorFlipVertical);
//		}
//		iActiveSchedulerWait->Start();
//	}

	Painter::Painter():
	mpPict(vNull),
	mpFont(vNull)
	{
	}

	TBool Sky::Painter::begin()
	{
		return vTrue;
	}

	TVoid Painter::end()
	{
		return;
	}

	TVoid Painter::drawLine( TPoint& start,TPoint& end )
	{
		::TPoint tStart(start.x,start.y);
		::TPoint tEnd(end.x,end.y);
		mpBitMapGC->DrawLine(tStart,tEnd);
	}

	TBool Painter::attachCanvas( Canvas* canvas )
	{
		mCanvas = canvas;
		Pict* iPict = canvas->getPict();
		if(iPict == vNull) {
			return vFalse;
		}
		mpBackBitmapDevice = CFbsBitmapDevice::NewL(iPict->getBitmap());
		if(mpBackBitmapDevice == vNull) {
			return vFalse;
		}
		mpBackBitmapDevice->CreateContext(mpBitMapGC);
		if(mpBitMapGC == vNull){
			return vFalse;
		}
		
		return vTrue;
	}

	Canvas* Painter::detachCanvas()
	{
		Canvas* p = mCanvas;
		//p->detachPicture();
		mCanvas = vNull;
		return p;
	}
		
	TVoid Painter::drawPict( TRect& dstRect,Pict* pict,TRect* srcRect )
	{
		xAssert(pict && pict->getBitmap());
		NType::TSize size = pict->getSize();
		::TRect symRect(0,0,size.w,size.h);
		if(pict->getBitmap())
		{
			if(pict->getBitmapMask())
			{
				mpBitMapGC->BitBltMasked(::TPoint(dstRect.x,dstRect.y),pict->getBitmap(),symRect,pict->getBitmapMask(),ETrue);
			}
			else
			{
				mpBitMapGC->BitBlt(::TPoint(dstRect.x,dstRect.y),pict->getBitmap(),symRect);
			}
		}
	}

	NType::TVoid Painter::drawText( NType::TRect& rect,const String* text,TColor color,Layout::Align Align_ )
	{
		setPenColor(color);
		::TRect r(rect.x,rect.y,rect.w + rect.x,rect.h + rect.y);
		TBuf<20> aBuf;
		CGraphicsContext::TTextAlign a;
		switch(Align_)
		{
		case Layout::left:
			a = CGraphicsContext::ELeft;
			break;
		case Layout::center:
			a = CGraphicsContext::ECenter;
			break;
		case Layout::right:
			a = CGraphicsContext::ERight;
			break;
		}
		TInt baselineOffset = mpFont->getFont().AscentInPixels() + ((rect.h - mpFont->getFont().HeightInPixels()) >> 1);
		mpBitMapGC->DrawText(TPtrC(text->toCharString()),::TPoint(50,50));//r,baselineOffset,a);
	}

	NType::TBool	Painter::attachFont(Font* iFont)
	{
		mpFont = iFont;
		mpBitMapGC->UseFont(&iFont->getFont());
		return vTrue;
	}

	Font* Painter::detachFont()
	{
		mpBitMapGC->DiscardFont();
		return mpFont;
	}

	NType::TBool Painter::attachPict( Pict* iPict )
	{
		if(iPict == vNull) {
			return vFalse;
		}
		if(mpPict != vNull){
			detachPict();
		}
		mpPict = iPict;
		mpBackBitmapDevice = CFbsBitmapDevice::NewL(iPict->getBitmap());
		if(mpBackBitmapDevice == vNull) {
			detachPict();
			return vFalse;
		}
		mpBackBitmapDevice->CreateContext(mpBitMapGC);
		if(mpBitMapGC == vNull){
			detachPict();
			return vFalse;
		}
		if(mpFont)
			mpBitMapGC->UseFont(&((Font*)mpFont)->getFont());
		return vTrue;
	}

	Pict* Painter::detachPict()
	{
		if(mpBackBitmapDevice)delete mpBackBitmapDevice;
		if(mpBitMapGC)delete mpBackBitmapDevice;
		mpBackBitmapDevice = vNull;
		mpBackBitmapDevice = vNull;
		Pict* p = mpPict;
		mpPict = vNull;
		return p;
	}

	NType::TVoid Painter::setPenColor( TColor color )
	{
		TRgb clr(color.r,color.g,color.b);
		mpBitMapGC->SetPenColor(clr);
	}

	NType::TVoid Painter::fillRect( TRect& rect )
	{
		::TRect aRect(rect.x,rect.y,rect.x+rect.w,rect.y+rect.h);
		mpBitMapGC->Clear(aRect);
	}

	NType::TVoid Painter::drawRect( TRect& rect )
	{
		::TRect aRect(rect.x,rect.y,rect.x+rect.w,rect.y+rect.h);
		mpBitMapGC->DrawRect(aRect);
	}

	NType::TVoid Painter::setBrushColor( TColor color )
	{
		TRgb clr(color.r,color.g,color.b);
		mpBitMapGC->SetBrushColor(clr);
	}

	NType::TBool Painter::fillWith(Pict* srcPict,NType::TRect* srcRect,NType::TRect* dstRect)
	{
		TInt xNumb,xRemain,yNum,yRemain;
		TInt xDst,yDst;
		TInt iImageW;
		TInt iImageH;
		CFbsBitGc * pFbsGc = mpBitMapGC;
		CFbsBitmap* srcBmp =  srcPict->getBitmap();
		CFbsBitmap* srcBmpMask = srcPict->getBitmapMask();

		if((vNull == srcBmp) || (vNull == srcBmpMask))
		{
			return vFalse;
		}

		iImageW = srcRect->w;
		iImageH = srcRect->h;

		xNumb = dstRect->w/iImageW;
		xRemain = dstRect->w%iImageW;

		yNum = dstRect->h/iImageH;
		yRemain =  dstRect->h%iImageH;

		::TRect tSrcRect(srcRect->x,srcRect->y,srcRect->x + srcRect->w,srcRect->y + srcRect->h);
		::TRect tRemain(srcRect->x,srcRect->y,srcRect->x + srcRect->w,srcRect->y + srcRect->h - yRemain);
		xDst = dstRect->x;
		for(TInt x = 0; x < xNumb; x++)
		{
			yDst = dstRect->y;
			for(TInt y = 0; y < yNum; y++)
			{
				pFbsGc->BitBltMasked(::TPoint(xDst,yDst),srcBmp,tSrcRect,srcBmpMask,ETrue);
				yDst += iImageH;
			}
			if(yRemain != 0)
			{
				pFbsGc->BitBltMasked(::TPoint(xDst,yDst),	srcBmp,tRemain,srcBmpMask,ETrue);    
			}
			xDst += iImageW;
		}
		yDst = dstRect->y;
		xDst = dstRect->x + dstRect->w - xRemain;
		if(xRemain != 0)
		{
			//
			tRemain.iBr.iX = srcRect->x + srcRect->w - xRemain;
			for(TInt i = 0; i < yNum; i++)
			{
				pFbsGc->BitBltMasked(::TPoint(xDst,yDst),srcBmp,tRemain,srcBmpMask,ETrue);   
				yDst += iImageH;
			}
			//
			if(yRemain != 0)
			{
				tRemain.iBr.iY = srcRect->y + srcRect->h - yRemain;
				pFbsGc->BitBltMasked(::TPoint(xDst,yDst),	srcBmp,	tRemain, srcBmpMask,ETrue);   
			}
		}
	}

	NType::TBool Painter::mirrorWith(Pict* srcPict,Pict* dst,TUint MirrorType_)
	{
//		CImageTransform *pImageTransform = new (OS::CImageTransform());
//		if(pImageTransform == vNull)
//			return vFalse;
//		CImageTransform::MirrorType t;
//		if(MirrorType_ == MirrorType_Vertical)
//			t = CImageTransform::VERTICAL;
//		else if(MirrorType_ == MirrorType_Horizontal)
//			t = CImageTransform::HORIZONTAL;
//		if(((myPict*)srcPict)->getBmp() != vNull && ((myPict*)dst)->getBmp() != vNull)
//			pImageTransform->Mirror(((myPict*)srcPict)->getBmp(),((myPict*)dst)->getBmp(),t);
//		if(((myPict*)srcPict)->getBmpMask() != vNull && ((myPict*)dst)->getBmpMask() != vNull)
//			pImageTransform->Mirror(((myPict*)srcPict)->getBmpMask(),((myPict*)dst)->getBmpMask(),t);
//		delete (pImageTransform);
		return vTrue;
	}

	TSize	Painter::calTextSize(String* str,Font* font)
	{
		TInt width = ((Font*)mpFont)->getFont().TextWidthInPixels(TPtrC((TUint16*)str->toCharString()));
		TInt height = ((Font*)mpFont)->getFont().HeightInPixels();
		return TSize(width,height);
	}

	OSPainter::~OSPainter()
	{
		if(mpBitMapGC)delete mpBitMapGC;
		if(mpBackBitmapDevice)delete mpBackBitmapDevice;
	}
	}
#endif //_SYMBIAN
